package com.gsafety.wellsfort.controller.organization;

import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.ExcelUtil;
import com.gsafety.wellsfort.domain.dto.ids.StudentIdDTO;
import com.gsafety.wellsfort.domain.model.organization.School;
import com.gsafety.wellsfort.domain.model.organization.Student;
import com.gsafety.wellsfort.domain.vo.StudentQueryVO;
import com.gsafety.wellsfort.domain.vo.StudentResultVO;
import com.gsafety.wellsfort.domain.vo.StudentVO;
import com.gsafety.wellsfort.service.organization.IStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/api/student")
@Api(value = "学生管理", description = "学生管理接口")
public class StudentController {
    @Autowired
    private IStudentService studentService;


    @ApiOperation(value = "学生家长列表")
    @PostMapping(value = "/list")
    public ApiResponse<List<StudentResultVO>> list(@RequestBody StudentQueryVO studentQueryVO) {
        return studentService.list(studentQueryVO);
    }

    @ApiOperation(value = "添加学生")
    @PostMapping(value = "/add")
    public ApiResponse add(@RequestBody StudentVO student) {
        return studentService.add(student);
    }


    /**
     * 学校批量添加
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "批量添加学生")
    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    public ApiResponse batchAdd(@RequestParam("fileName") MultipartFile file, Integer schoolId, Integer classId) throws Exception {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 用uuid作为文件名，防止生成的临时文件重复
        final File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
        // MultipartFile to File
        file.transferTo(excelFile);
        List<StudentVO> list = ExcelUtil.getDtoList(excelFile, StudentVO.class);
        // 程序结束时，删除临时文件
        deleteFile(excelFile);
        return studentService.batchAddStudent(list,schoolId,classId);
    }

    @ApiOperation(value = "更新学生")
    @PostMapping(value = "/update")
    public ApiResponse update(@RequestBody StudentVO student) {
        return studentService.update(student);
    }

    @ApiOperation(value = "删除学生")
    @PostMapping(value = "/delete")
    public ApiResponse delete(@RequestBody StudentIdDTO idDto) {
        return studentService.delete(idDto.getStudentId());
    }

    /**
     * 删除
     *
     * @param files
     */
    private void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
